/**
 * 商品图片模块数据操作文件
 */

const { Op } = require('sequelize');
const { sequelize } = require('./common');
const { image_list } = sequelize.models;


/**
 * 添加
 * @param {*} model 
 * @returns 
 */
const add = model => image_list.bulkCreate(model);

/**
 * 删除
 * @param {*} id 
 * @returns 
 */
const del = id => image_list.destroy({ where: { id } });

/**
 * 获取所有
 * @param {*} user_id 
 * @returns 
 */
const getAll = ({ pageNo, pageSize, user_id, start_date, end_date }) => image_list.findAndCountAll({
    limit: pageSize * 1,
    offset: (pageNo - 1) * pageSize,
    order: [['id', 'desc']],
    where: {
        user_id,
        ...(start_date && {
            create_time: {
                [Op.gte]: start_date
            }
        }),
        ...(end_date && {
            create_time: {
                [Op.lt]: end_date
            }
        })
    }
});

module.exports = {
    add,
    del,
    getAll,
}